package mpcs.model
{
	import mpcs.NotiConst;
	import mpcs.model.vo.BasicInfoVO;
	import mpcs.model.vo.ContactInfoVO;
	import mpcs.model.vo.GlobalVO;
	import mpcs.model.vo.MobileVO;
	import mpcs.model.vo.UserVO;
	import mpcs.mylibs.data.SimpleCmd;
	import mpcs.mylibs.data.SimpleMsg;
	import mpcs.mylibs.events.GenericEvent;
	import mpcs.mylibs.interfaces.IConnection;
	import mpcs.mylibs.manager.ConManager;
	import mpcs.utils.TrimUtils;
	
	import mx.controls.Alert;
	
	import org.puremvc.as3.patterns.proxy.Proxy;
	
	public class UserInfoProxy extends Proxy {
		
		public static const NAME:String = "UserInfoProxy";
		private var con:IConnection = ConManager.connection;
		private var basicInfo:BasicInfoVO;
		private var contactInfo:ContactInfoVO;
		private var newPwd:String;
		
		public function UserInfoProxy() {
			super( NAME );
			
			con.addEventListener("500201", rBasicInfo);
			con.addEventListener("500202", rSaveBasicInfo);
			con.addEventListener("500203", rContactInfo);
			con.addEventListener("500204", rSaveContactInfo);
			con.addEventListener("500205", rUpdatePwd);
			con.addEventListener("500207", rSaveInitPosition);
			con.addEventListener("500209", rMobileList);
		}
		
		// 客户登录请求用户基本信息
		public function sBasicInfo(uservo:UserVO):void{
			trace("UserInfoProxy ...  sBasicInfo ... " + uservo.toString());
			var cmd:SimpleCmd = new SimpleCmd(100201);
			cmd.writeString(uservo.getEmail());
			con.send(cmd);
		}
		
		// 返回用户基本信息
		private function rBasicInfo(evt:GenericEvent):void {
			var msg:SimpleMsg = evt.getMsg() as SimpleMsg;
			if(msg.getHeadData(1) == 0) {
//				trace(" 返回用户 " + msg.readString() + " 基本信息成功 ... rBasicInfo ... UserInfoProxy ... ");
				if(msg.readString() != GlobalVO.userVO.getEmail())
					return;
				GlobalVO.userVO.getBasicInfoVO().setUserName(TrimUtils.Trim(msg.readString()));
				GlobalVO.userVO.getBasicInfoVO().setGender(TrimUtils.Trim(msg.readString()));
				GlobalVO.userVO.getBasicInfoVO().setBirthday(TrimUtils.Trim(msg.readString()));
				GlobalVO.userVO.getBasicInfoVO().setBloodType(TrimUtils.Trim(msg.readString()));
				GlobalVO.userVO.getBasicInfoVO().setMarriage(TrimUtils.Trim(msg.readString()));
				GlobalVO.userVO.getBasicInfoVO().setCareer(TrimUtils.Trim(msg.readString()));
				GlobalVO.userVO.getBasicInfoVO().setEducation(TrimUtils.Trim(msg.readString()));
				GlobalVO.userVO.getBasicInfoVO().setResidence(TrimUtils.Trim(msg.readString()));
				GlobalVO.userVO.getBasicInfoVO().setHome(TrimUtils.Trim(msg.readString()));
				GlobalVO.userVO.getBasicInfoVO().setIdCard(TrimUtils.Trim(msg.readString()));
				trace( GlobalVO.userVO.toString() );
				sendNotification(NotiConst.R_BASICINFO_SUCCESS);
			}else{
				sendNotification(NotiConst.R_BASICINFO_FAILED, msg.getHeadDatas());
			}
		}
		
		// 客户登录请求用户联系信息
		public function sContactInfo(uservo:UserVO):void{
			trace("UserInfoProxy ...  rContactInfo ... " + uservo.toString());
			var cmd:SimpleCmd = new SimpleCmd(100203);
			cmd.writeString(uservo.getEmail());
			con.send(cmd);
		}
		
		// 返回用户联系信息
		private function rContactInfo(evt:GenericEvent):void {
			var msg:SimpleMsg = evt.getMsg() as SimpleMsg;
			if(msg.getHeadData(1) == 0) {
				GlobalVO.userVO.getContactInfoVO().setQQ(TrimUtils.Trim(msg.readString()));
				GlobalVO.userVO.getContactInfoVO().setMSN(TrimUtils.Trim(msg.readString()));
				GlobalVO.userVO.getContactInfoVO().setMobile(TrimUtils.Trim(msg.readString()));
				GlobalVO.userVO.getContactInfoVO().setTel(TrimUtils.Trim(msg.readString()));
				GlobalVO.userVO.getContactInfoVO().setZip(TrimUtils.Trim(msg.readString()));
				trace( GlobalVO.userVO.toString() );
				sendNotification(NotiConst.R_CONTACTINFO_SUCCESS);
			}else{
				sendNotification(NotiConst.R_CONTACTINFO_FAILED, msg.getHeadDatas());
			}
		}
		
		// 客户登录请求保存用户基本信息
		public function sSaveBasicInfo(vo:BasicInfoVO):void{
			basicInfo = vo;
			trace("UserInfoProxy ...  sSaveBasicInfo ... " + GlobalVO.userVO.getEmail() + vo.toString());
			var cmd:SimpleCmd = new SimpleCmd(100202);
			cmd.writeString(GlobalVO.userVO.getEmail());
			cmd.writeString(vo.getUserName());
			cmd.writeString(vo.getGender());
			cmd.writeString(vo.getBirthday());
			cmd.writeString(vo.getBloodType());
			cmd.writeString(vo.getMarriage());
			cmd.writeString(vo.getCareer());
			cmd.writeString(vo.getEducation());
			cmd.writeString(vo.getResidence());
			cmd.writeString(vo.getHome());
			cmd.writeString(vo.getIdCard());
			con.send(cmd);
		}
		
		// 保存用户基本信息返回
		private function rSaveBasicInfo(evt:GenericEvent):void {
			var msg:SimpleMsg = evt.getMsg() as SimpleMsg;
			if(msg.getHeadData(1) == 0) {
//				trace(" 保存用户 " + msg.readString() + " 基本信息成功 ... rSaveBasicInfo ... UserInfoProxy ... ");
				// 在此将客户端用户信息与数据库中的用户信息保持同步
				GlobalVO.userVO.setBasicInfoVO(basicInfo);
//				trace( GlobalVO.userVO.toString() );
				sendNotification(NotiConst.R_SAVE_BASICINFO_SUCCESS, msg.readString());
			}else{
				sendNotification(NotiConst.R_SAVE_BASICINFO_FAILED, msg.getHeadDatas());
			}
		}
		
		// 客户登录请求保存用户联系信息
		public function sSaveContactInfo(vo:ContactInfoVO):void{
			contactInfo = vo;
			trace("UserInfoProxy ...  sSaveContactInfo ... " + GlobalVO.userVO.getEmail() + vo.toString());
			var cmd:SimpleCmd = new SimpleCmd(100204);
			cmd.writeString(GlobalVO.userVO.getEmail());
			cmd.writeString(vo.getQQ());
			cmd.writeString(vo.getMSN());
			cmd.writeString(vo.getMobile());
			cmd.writeString(vo.getTel());
			cmd.writeString(vo.getZip());
			con.send(cmd);
		}
		
		// 保存用户联系信息返回
		private function rSaveContactInfo(evt:GenericEvent):void {
			var msg:SimpleMsg = evt.getMsg() as SimpleMsg;
			if(msg.getHeadData(1) == 0) {
				// 在此将客户端用户信息与数据库中的用户信息保持同步
				GlobalVO.userVO.setContactInfoVO(contactInfo);
//				trace( GlobalVO.userVO.toString() );
				sendNotification(NotiConst.R_SAVE_CONTACTINFO_SUCCESS, msg.readString());
			}else{
				sendNotification(NotiConst.R_SAVE_CONTACTINFO_FAILED, msg.getHeadDatas());
			}
		}
		
		// 客户请求修改密码
		public function sUpdatePwd(renewPwd:String):void{
			trace("UserInfoProxy ...  sUpdatePwd ... ");
			newPwd = renewPwd;
			var cmd:SimpleCmd = new SimpleCmd(100205);
			cmd.writeString(GlobalVO.userVO.getEmail());
			cmd.writeString(renewPwd);
			con.send(cmd);
		}
		
		// 修改密码返回
		private function rUpdatePwd(evt:GenericEvent):void {
			var msg:SimpleMsg = evt.getMsg() as SimpleMsg;
			if(msg.getHeadData(1) == 0) {
				// 保持客户端数据与服务器数据同步
				GlobalVO.userVO.setPassword(newPwd);
				sendNotification(NotiConst.R_UPDATEPWD_SUCCESS, msg.readString());
			}else{
				sendNotification(NotiConst.R_UPDATEPWD_FAILED, msg.getHeadDatas());
			}
		}
		
		// 客户请求设定手机的初始位置
		public function sSaveInitPosition(obj:Object):void{
			trace("UserInfoProxy ...  sSaveInitPosition ... ");
			var cmd:SimpleCmd = new SimpleCmd(100207);
			cmd.writeString(GlobalVO.userVO.getMobileList()[0].getMobile());
			cmd.writeString(obj.lat);
			cmd.writeString(obj.lng);
			//此处作为以后扩展设置最大便宜距离,在此默认设置为1000米
			cmd.writeString("1000");
			con.send(cmd);
		}
		
		// 请求设定手机的初始位置返回
		private function rSaveInitPosition(evt:GenericEvent):void {
			var msg:SimpleMsg = evt.getMsg() as SimpleMsg;
			if(msg.getHeadData(1) == 0) {
				sendNotification(NotiConst.R_SAVE_INIT_POSITION_SUCCESS, msg.readString());
			}else{
				sendNotification(NotiConst.R_SAVE_INIT_POSITION_FAILED, msg.getHeadDatas());
			}
		}		
		
		// 客户请求绑定手机列表
		public function sMobileList():void{
			trace("UserInfoProxy ...  sMobileList ... ");
			var cmd:SimpleCmd = new SimpleCmd(100209);
			cmd.writeString(GlobalVO.userVO.getEmail());
			con.send(cmd);
		}
		
		// 请求绑定手机列表返回
		private function rMobileList(evt:GenericEvent):void {
			var msg:SimpleMsg = evt.getMsg() as SimpleMsg;
			if(msg.getHeadData(1) == 0) {
				var num:int = msg.readInt();
				var mobileList:Array = [];
				trace("绑定手机列表个数 = " + num );
				// 循环读取用户绑定的手机列表
				for(var i:int = 0 ; i < num ; i++){
					var vo:MobileVO = new MobileVO();
					vo.setMobile(TrimUtils.Trim(msg.readString()));
					vo.setRelationship(TrimUtils.Trim(msg.readString()));
					vo.setStatus(TrimUtils.Trim(msg.readString()));
					vo.setBelongto(TrimUtils.Trim(msg.readString()));
					vo.setBegintime(TrimUtils.Trim(msg.readString()));
					vo.setType(TrimUtils.Trim(msg.readString()));
					vo.setLongitude(TrimUtils.Trim(msg.readString()));
					vo.setLatitude(TrimUtils.Trim(msg.readString()));
					vo.setRadius(TrimUtils.Trim(msg.readString()));
					mobileList.push(vo);
				}
				/*if(num > 0){
					// 保持客户端数据与服务器数据同步
					GlobalVO.userVO.setMobileVO(mobileList[0]);
				}*/
				GlobalVO.userVO.setMobileList(mobileList);
				sendNotification(NotiConst.R_MOBILELIST_SUCCESS);
			}else{
				sendNotification(NotiConst.R_MOBILELIST_FAILED);
			}
		}
		
	}
}